package Review;

public class Test06 {

	public static void main(String[] args) 
	{
		int[] pre={1,2,4,7,3,5,6,8};
		int[] in = {4,7,2,1,5,3,8,6};
		System.out.println(reconstructBinaryTree(pre,in).left.value);
	}
	public static BinaryTreeNode reconstructBinaryTree(int[] pre,int[]in){
		if(pre==null||in==null)
			return null;
		
		return reconstructBinaryTree(pre, 0,pre.length-1,in,0,in.length-1);
	}
	public static BinaryTreeNode reconstructBinaryTree(int[]pre,int startPre,int endPre,int[]in,int startIn,int endIn){
		if(startPre>endPre||startIn>endIn)
			return null;
		BinaryTreeNode root = new BinaryTreeNode();
		root.value = pre[startPre];
		for(int i=startIn;i<=endIn;i++){
			if(in[i]==pre[startPre]){
				root.left=reconstructBinaryTree(pre, startPre+1, i-startIn+startPre, in, startIn, i-1);
				root.right=reconstructBinaryTree(pre, i-startIn+startPre+1, endPre, in, i+1, endIn);
			}
				
		}
		return root;
	}

}
